const express = require('express')

const mysql = require('../database/index')

const router = express.Router()

const multer = require('multer')

// 设置参数
const upload = multer({dest:'uploads/'})


//路由
// 配置formdata类型的局部中间件
//获取文章列表数据
// router.get('/article/add',upload.single('cover_img'),(req,resp)=>{
//     console.log(req.file);  //文件信息
//     console.log('-------');
//     console.log(req.body);  //普通字段
//     // let sql = ``
//     resp.send('获取文章列表数据成功')
// })


router.post('/article/add', upload.single('cover_img'), (req, res) => {
    // console.log(req.file);// 文件信息
    // console.log('---------');
    // console.log(req.body);// 普通字段
    // 准备各各字段值
    let { title,cate_id,content,state } = req.body;
    let cover_img = "/uploads/" + req.file.filename;
    let author_id = req.user.id;
    let pub_date = '1111-11-11 11:11:11';
    let is_delete = 0;
    // 生成sql
    let sql = `insert into article set title="${title}",cate_id="${cate_id}",content="${content}",state="${state}",cover_img="${cover_img}",author_id="${author_id}",pub_date="${pub_date}",is_delete="${is_delete}"`;
    // 执行
    mysql(sql, (err, results) => {
        // 判断sql
        if (err) throw err;
        // 判断结果
        if (results.affectedRows !== 1) return res.send({status: 1, message: '添加失败！'})
        // 成功提示
        res.send({status: 0, message: '发布文章成功！'});
    });
    // res.send("文章发布成功");
});
module.exports=router
